package de.szut.scene.scenes;

import de.szut.fragments.dialogs.StartGameDialog;
import de.szut.scene.helper.BaseScene;
import de.szut.scene.helper.SceneManager;
import de.szut.scene.helper.SceneType;
import de.szut.scene.helper.StorageHelper;
import de.szut.util.Settings;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.TextMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;

/**
 * Created with IntelliJ IDEA.
 * User: nbothmer
 * Date: 27.08.13
 * Time: 15:04
 * To change this template use File | Settings | File Templates.
 */
public class MenuScene extends BaseScene implements org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener {

    private Sprite menu;
    private org.andengine.entity.scene.menu.MenuScene menuChildScene;
    private final int MENU_PLAY = 0;
    private final int MENU_OPTIONS = 1;
    private StorageHelper storageHelper;
    private StartGameDialog startGameDialog;

    @Override
    public void createScene() {
        createBackground();
        createMenuChilds();
        storageHelper = StorageHelper.getInstance(resourcesManager.getActivity(), resourcesManager.getDbHelper());

    }

    @Override
    public void onBackKeyPressed() {
        System.exit(0);
    }

    @Override
    public SceneType getSceneType() {
        return SceneType.SCENE_MENU;
    }

    @Override
    public void disposeScene() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    private void createBackground() {
        menu = new Sprite(0, 0, this.resourcesManager.getMenuRegion_background(), vbom) {
            @Override
            protected void preDraw(GLState pGLState, Camera pCamera) {
                super.preDraw(pGLState, pCamera);
                pGLState.enableDither();
            }
        };
        menu.setPosition((Settings.RESOLUTION_WIDTH * 0.5f) - (menu.getWidth() * 0.5f), (Settings.RESOLUTION_HEIGHT * 0.5f) - (menu.getHeight() * 0.5f));
        attachChild(menu);
    }

    private void createMenuChilds() {
        menuChildScene = new org.andengine.entity.scene.menu.MenuScene(camera);
        menuChildScene.setPosition(10, 140);

        final IMenuItem menuItemPlay = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_PLAY, resourcesManager.getMenu_button(), vbom), 1.2f, 1);
        final IMenuItem menuItemOptions = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_OPTIONS, resourcesManager.getMenu_button(), vbom), 1.2f, 1);
        final TextMenuItem textBtnPlay = new TextMenuItem(MENU_PLAY, resourcesManager.getMenuSceneFont(), "PLAY", vbom);
        final TextMenuItem textBtnOptions = new TextMenuItem(MENU_OPTIONS, resourcesManager.getMenuSceneFont(), "OPTIONS", vbom);
        menuItemPlay.attachChild(textBtnPlay);
        menuItemOptions.attachChild(textBtnOptions);
        menuChildScene.addMenuItem(menuItemPlay);
        menuChildScene.addMenuItem(menuItemOptions);

        menuChildScene.buildAnimations();
        menuChildScene.setBackgroundEnabled(false);

        menuItemPlay.setPosition(menuItemPlay.getX(), menuItemPlay.getY() - 100);
        menuItemOptions.setPosition(menuItemOptions.getX(), menuItemOptions.getY() - 100);
        textBtnPlay.setPosition(menuItemPlay.getScaleCenterX() - (textBtnPlay.getWidth() / 2), menuItemPlay.getScaleCenterY() - (textBtnPlay.getHeight() / 2));
        textBtnOptions.setPosition(menuItemOptions.getScaleCenterX() - (textBtnOptions.getWidth() / 2), menuItemOptions.getScaleCenterY() - (textBtnOptions.getHeight() / 2));

        menuChildScene.setOnMenuItemClickListener(this);

        setChildScene(menuChildScene);
    }

    public void deleteCurrentGameState(){

    }

    @Override
    public boolean onMenuItemClicked(org.andengine.entity.scene.menu.MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY) {
        switch (pMenuItem.getID()) {
            case MENU_PLAY:
                if (storageHelper.gotNonSavedGameState()) {
                    SceneManager.getInstance().createGameScene();
                }else{
                    startGameDialog = StartGameDialog.newInstance();
                    startGameDialog.show(resourcesManager.getFragmentManager(),"startDlg");
                }
                return true;
            case MENU_OPTIONS:
                return true;
            default:
                return false;
        }
    }
}
